/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-08-15 16:28:18
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-09-25 16:55:14
 * @FilePath: \cxl-admin\src\pages\company\audit\list\index.ts
 */

import { companyAuditList, enabledCompany, searchAuditParamsInterface } from '@/apis/company/audit'
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { ElMessage } from 'element-plus'
import { reactive } from 'vue'
import * as tableConfig from './config.json'

export class CompanyAuditData extends BaseData implements BaseMethod {
    //弹窗数据
    dialogData: any = reactive({
        visible: false,
        title: '审核',
        type: 'audit',
        config: {}
    })

    //搜索结构数据
    searchResult: any = {
        company: '',
        phone: '',
        name: '',
        auditStatus: '',
        useStatus: ''
    }

    constructor() {
        super(tableConfig)
        this.getData()
    }
    /**
     * 监听点击搜索按钮
     * @param config
     */
    onSearch = (config: any) => {
        this.searchResult = config
        this.searchResult.company = config.custom.companyName
        this.getData()
    }

    /**
     * 监听点击搜索重置按钮
     */
    onReset = () => {
        this.pageData.pagination.pageNo = 1
        this.searchResult = {
            company: '',
            phone: '',
            name: '',
            auditStatus: '3',
            useStatus: ''
        }
        this.getData()
    }

    /**
     * 监听分页发生改变
     */
    onPaginationChange = (index: number) => {
        this.pageData.pagination.pageNo = index
        this.getData()
    }

    /**
     * 监听选择每页显示个数
     * @param size
     */
    onPaginationSize = (size: number) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = size
        this.getData()
    }

    /**
     * 拉取数据
     * @param config
     */
    getData = () => {
        const params: searchAuditParamsInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize,
            companyName: this.searchResult.company,
            phone: this.searchResult.phone
        }
        //默认审核状态是(3全部；0待审核；1.通过；2.未通过)）
        if (Number(this.searchResult.auditStatus) !== 3) {
            params.auditStatus = this.searchResult.auditStatus
        }
        companyAuditList(params).then(res => {
            if (res.data.value === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.data.total
                const arr: any[] = res.data.data.records
                const ids: string[] = []
                arr.forEach((element: any) => {
                    element.status = element.companyEnabledStatus
                    ids.push(element.companyId)
                })
                this.pageData.table.data.push(...arr)
            }
        })
    }

    /**
     * 监听点击的时编辑按钮/查看按钮
     * @param type
     * @param index
     */
    onOperation = (result: any) => {
        const { type, data } = result
        console.log(data)
        if (type === 'switch') {
            enabledCompany(data.companyId, data.status).then(res => {
                console.log(res.data)
                if (res.data.value === 30007) {
                    ElMessage.error('已存在管理员！')
                } else if (res.data.value === 200) {
                    ElMessage.success('操作成功')
                }
                this.getData()
            })
        } else {
            this.dialogData.visible = true
            this.dialogData.type = type
            this.dialogData.config = data
        }
    }

    /**
     * 监听弹窗隐藏
     */
    onDialogClose() {
        this.dialogData.visible = false
        //todo 弹窗隐藏后，重新拉取数据
        // type === audit 则重新拉取数据
        if (this.dialogData.type === 'audit') {
            this.getData()
        }
    }
    /**
     * 账号启用禁用
     * @param companyId
     * @param status
     */
    onStatusChange = (companyId: string, status: boolean) => {
        enabledCompany(companyId, status).then(res => {
            console.log(res.data)
            if (res.data.value === 30007) {
                ElMessage.error('已存在管理员！')
            } else if (res.data.value === 200) {
                ElMessage.success('操作成功')
            }
            this.getData()
        })
    }
}
